
// <copyright file="DynamicToStringHelperExtensions.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The dynamic to string helper extensions.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Extensions
{
    using System;
    using System.Reflection;

    using SubhadraSolutions.Sharp.Utils.Reflection;

    /// <summary>
    /// The dynamic to string helper extensions.
    /// </summary>
    public static class DynamicToStringHelperExtensions
    {
        #region Public Methods and Operators

        /// <summary>
        /// The export as string.
        /// </summary>
        /// <param name="o">
        /// The o.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string ExportAsString(this object o)
        {
            if (o != null)
            {
                Type t = typeof(DynamicToStringHelper<>).MakeGenericType(new[] { o.GetType() });
                return t.InvokeMember("ExportAsString", BindingFlags.InvokeMethod, null, null, new[] { o }) as string;
            }

            return null;
        }

        /// <summary>
        /// The export as string.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string ExportAsString<T>(this T obj)
        {
            return DynamicToStringHelper<T>.ExportAsString(obj);
        }

        /// <summary>
        /// The export type unknown object as string.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <param name="format">
        /// The format.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string ExportTypeUnknownObjectAsString(this object obj, string format)
        {
            if (obj != null)
            {
                Type t = typeof(DynamicToStringHelper<>).MakeGenericType(new[] { obj.GetType() });
                return t.InvokeMember("ExportAsString", BindingFlags.InvokeMethod, null, null, new[] { obj, format }) as string;
            }

            return null;
        }

        #endregion Public Methods and Operators
    }
}
